package com.colabo.j2ee.web.oaf.monitorperspective.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONArray;

import com.colabo.j2ee.web.core.context.ContextUtil;
import com.colabo.j2ee.web.core.exception.ServiceException;
import com.colabo.j2ee.web.oaf.security.ISecurityStrategyGroupService;


public class SecurityStrategyViewAction {
	private static final String SYSTEM_SECUTIRY_LEVEL = "system_secutiry_level";
	
	ISecurityStrategyGroupService ipsecurityGroupService = (ISecurityStrategyGroupService) ContextUtil.getBean(ISecurityStrategyGroupService.BEAN_ID);
	
	public String loadSecurityStrategyGroup() throws Exception{

			// 获取安全策略组名称信息，并将对应信息保存入 安全策略组名 数据集，完成数据集数据初始化
			Object[] result = ipsecurityGroupService.initData(5);
			List resultvalue = (List) result[0];
			List securityStrategyGroupBeans = new ArrayList();
			for (int i = 0; i < resultvalue.size(); i++) {
				Map map = new HashMap();
				Object[] obj = (Object[]) resultvalue.get(i);
				if (i == 0) {
				}
				map.put("oldGroupName", obj[0]);
				map.put("groupName", obj[0]);
				map.put("modifyflag", obj[1]);
				map.put("isdefault", (obj[2] == null || obj[2].equals("")) || obj[2].equals("0") ?  false: true);
				securityStrategyGroupBeans.add(map);
			}
			JSONArray json = JSONArray.fromObject(securityStrategyGroupBeans);
			String ret = json.toString();
			return ret;
	}
	
	/**
	 *  According to the given security strategy group name, retrieve the group's detail
	 * @param ssGroupName given group name
	 * @return json string of group's detail
	 * @throws ServiceException 
	 */
	public String loadSecurityStrategyGroupDetails(String ssGroupName) throws ServiceException {
		String groupname = ssGroupName;
		// 准备参数,调用service中的数据查询方法
		Map params = new HashMap();
		params.put("groupName", groupname);
		
		String sysvalue = ipsecurityGroupService.getStrategyValue(SYSTEM_SECUTIRY_LEVEL);
		String sysgroupName = null;
		
		if("1".equals(sysvalue)){
			sysgroupName = "一级安全策略标准";
		}else if("2".equals(sysvalue)){
			sysgroupName = "二级安全策略标准";
		}else if("3".equals(sysvalue)){
			sysgroupName = "三级安全策略标准";
		}else{
			sysgroupName = "一级安全策略标准";
		}
		params.put("sysgroupName", sysgroupName);

		Object[] result = ipsecurityGroupService.queryDetailData(params, 0);

		// 将查询出来的明细数据进行处理，将对应的安全策略描述保存入安全策略组对象
		List list = (List) result[0];
		JSONArray json = JSONArray.fromObject(list);
		String ret = json.toString();
		return ret;
	}
	
	public String loadSecurityStrategyMatrixDetails() throws ServiceException {
		List list = ipsecurityGroupService.queryForStrategyMatrix();
		JSONArray json = JSONArray.fromObject(list);
		String ret = json.toString();
		return ret;
	}
}
